package cn.fengzhilun.javase.day02;

import java.util.Arrays;

import org.junit.Test;

public class Test1 {
     @Test
     public void test() {
    	 //int a[]= {1,2,3,4,5,6};
    	 //System.out.println(linearSearch(a,10));
    	 //System.out.println(linearSearch(a,3));
    	int[] a1 = {1, 5, 16}, a2 = {3, 4, 6, 7};
    	 testSortTwoSortedArray(a1,a2);
    	// int a[]= {2,7,3,4,1,5};
    	 //selectionSort(a);
    	 
     }
	//线形查找
	public int linearSearch(int a[],int x) {
		for(int i=0;i<a.length-1;i++) {
			if(x==a[i]) {
				return i;
			}
		}
		return 0;
	}
	//二分查找
	public int binarySearch(int a[],int x) {
		int low =0 ;
		int high=a.length-1;
		while(low<=high){
			int mid =(low+high)/2;
			if(x==a[mid]) {
				return mid;
				
			}else if(a[mid]<x) {
				low =mid+1;
				
			}else {
				high=mid-1;
				
			}
			
		}
		return 0;
		
		
	}
	//合并排序的数组
	public static void testSortTwoSortedArray(int a1[],int a2[]) {
		int n1=a1.length;
		int n2=a2.length;
		int i=0,j=0,k=0;
		int a3[]=new int [n1+n2];
		while(i<n1&&j<n2) {
			if(a1[i]<a2[j]) {
				a3[k]=a1[i];
				i++;
				k++;
				
			}else if(a1[i]==a2[j]){
				a3[k]=a2[j];
				k++;
				j++;
				
			}else {
				a3[k]=a2[j];
				k++;
				j++;
			}
		}
		while(i<n1) {
			a3[k]=a1[i];
			k++;
			i++;
			
		}
		while(j<n2) {
			a3[k]=a2[j];
			k++;
			j++;
		}
		System.out.println(Arrays.toString(a3));
	}
	//选择排序
	public static void selectionSort(int a[]) {
		System.out.println(Arrays.toString(a));

		int key;
		for(int i=0;i<a.length-1;i++) {
			key=i;
			for(int j=i+1;j<a.length;j++) {
				if(a[key]>=a[j]) {
					key=j;
					System.out.println(a[key]);
					
				}
				
				
			}
			int temp=a[i];
			a[i]=a[key];
			a[key]=temp;
		}
		System.out.println(Arrays.toString(a));
	}
	//插入排序
	
	
}
